package com.avocado.objectstore.controller.file;

import com.avocado.commons.model.http.BaseResponse;
import com.avocado.commons.utils.JsonUtils;
import com.avocado.base.service.UserService;
import com.avocado.objectstore.dto.file.FileDto;
import com.avocado.objectstore.dto.file.UploadFileDto;
import com.avocado.objectstore.service.ObjectStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/file/*")
@Slf4j
public class FileController {



    @Autowired
    private UserService userService;

    @Autowired
    private ObjectStoreService objectStoreService;



    @RequestMapping("/list")
    public @ResponseBody BaseResponse list(Principal user,String currentDirectory)throws Exception{

        Long userId=userService.findByName(user.getName()).getId();


        return BaseResponse.builder().data(objectStoreService.list(userId,currentDirectory)).status(1).message("操作成功！").build();

    }

    @RequestMapping("/upload")
    public @ResponseBody BaseResponse upload(Principal user, String requestJson) throws Exception {

        UploadFileDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, UploadFileDto.class);

        Long userId=userService.findByName(user.getName()).getId();

        return BaseResponse.builder().data(objectStoreService.upload(userId,dto)).status(1).message("操作成功！").build();

    }

    @RequestMapping("/multipart/upload")
    public @ResponseBody BaseResponse uploadMultipartFiles(Principal user, String currentDirectory,@RequestParam MultipartFile[] multipartFiles ) throws Exception {

        Long userId=userService.findByName(user.getName()).getId();
        return BaseResponse.builder().data(objectStoreService.uploadMultipartFiles(userId,currentDirectory,multipartFiles)).status(1).message("操作成功！").build();

    }

    @RequestMapping("/delete")
    public @ResponseBody BaseResponse delete(Principal user,String requestJson) throws Exception {

        FileDto dto = JsonUtils.jsonToObjectThrowsException(requestJson, FileDto.class);

        Long userId=userService.findByName(user.getName()).getId();

        objectStoreService.delete(userId,dto);

        return BaseResponse.builder().status(1).message("操作成功！").build();
    }


    @RequestMapping("/directory/create")
    public @ResponseBody BaseResponse createDirectory(Principal user,String directoryPath)throws Exception {

        Long userId=userService.findByName(user.getName()).getId();

        objectStoreService.createDirectory(userId,directoryPath);

        return BaseResponse.builder().status(1).message("操作成功！").build();
    }


    @RequestMapping("/rename")
    public @ResponseBody BaseResponse rename(Principal user,String oldFilePath,String newFilePath) throws Exception{

        Long userId=userService.findByName(user.getName()).getId();

        objectStoreService.rename(userId,oldFilePath,newFilePath);

        return BaseResponse.builder().status(1).message("操作成功！").build();

    }

}
